<?php

namespace App\Http\Controllers\Admin;

use Cartalyst\Sentry\Facades\Laravel\Sentry;
use App\Http\Model\Auth;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class IndexController extends CommonController
{
    public function index(){
        $title="管理面板";
		$user = Sentry::getUser();
		$nickname=$user->nickname;
		$groups = $user->getGroups();
		
		//顶部菜单
		if($user->id==1){
			$cates=Auth::where(['a_id'=>0,'showed'=>1,'activated'=>1])->orderBy('orders','asc')->get();
		}else{
			// 获得合并后的权限信息
			$permissions = $user->getMergedPermissions();
			foreach($permissions as $k=>$v){
				if($v==1){
					$key[]=$k;
				}
			}
			$cate=Auth::where(['a_id'=>0,'showed'=>1,'activated'=>1])->orderBy('orders','asc')->get();
			$cates=[];
			foreach($cate as $r){
				if(in_array($r->role,$key)){
					$cates[]=$r;
				}
			}
		}
        return view('admin.index',compact('title','nickname','groups','cates'));
    }

    /**
     * @return 主面板
     */
    public function info(){
        return view('admin.info');
    }

    /**
     * 修改密码
     */
    public function pass(Request $request){
        if($request->isMethod('post')){
        	$input=Input::all();
            $rules=[
                'password_o'=>'required',
                'password'=>'required|between:6,20|confirmed',
            ];
            $message=[
                'password_o.required'=>'旧密码不能为空！',
                'password.required'=>'新密码不能为空！',
                'password.between'=>'新密码范围必须在6-20位之间！',
                'password.confirmed'=>'新密码和确认密码不一致！',
            ];
            $validator=Validator::make($input,$rules,$message);
            if($validator->passes()){
            	$ret=[
					"status"=>300,
					"msg"=>'修改密码失败,请重试!'
				];
                try
                {
                    $user = Sentry::getUser();
                    // 获得当前登陆的用户
                    if(Sentry::checkPassword($input['password_o'])){
                        $resetCode = $user->getResetPasswordCode();
                        // 检查重置密码的代码是否有效
                        if ($user->checkResetPasswordCode($resetCode))
                        {
                            // 重置用户密码
                            if ($user->attemptResetPassword($resetCode,$input['password']))
                            {
				            	$ret['status']=200;
				            	$ret['msg']='修改密码成功!';
                            }else{
								$ret['msg']='修改密码失败,请重试!';
                            }
                        }else{
                        	$ret['msg']='修改密码失败,修改方式无效,请重试!';
                        }
                    }else{
                    	$ret['msg']='原密码错误!';
                    }
                }

                catch (\Cartalyst\Sentry\Users\UserExistsException $e){
                	$ret['msg']='修改密码失败,无效Login,请重试!';
                }
                catch (\Cartalyst\Sentry\Users\UserNotFoundException $e){
                	$ret['msg']='修改密码失败,用户获取失效,请重试!';
                }
				return $ret;
            }else{
				$ret=[
					"status"=>300,
					"msg"=>$validator->messages()->first()
				];
                return $ret;
            }
        }else{
        	$user = Sentry::getUser();
			$groups = $user->getGroups();
            return view('admin.pass',compact('user','groups'));
        }
    }
}
